package com.gict.antifraudback.utils;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 磁力获取工具类
 */
public class MagnetUtil {


    /**
     * 爬虫抓取链接超时时间，单位：毫秒
     */
    public static final int SPIDER_TIMEOUT = 10000;
    /**
     * 爬虫默认的userAgent
     */
    public static final String USER_AGENT = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36 Edg/113.0.1774.35";

    private static Map<String, String> map = new HashMap<>();

    static {
        map.put("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0");
        map.put("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7");
        map.put("Cookie", "cPNj_2132_saltkey=ujN4JdmS; _safe=vqd37pjm4p5uodq339yzk6b7jdt6oich; cPNj_2132_lastvisit=1715840410; cPNj_2132_lastact=1715844010%09index.php%09; cPNj_2132_lastfp=27cf7dc10da7360a67316c1c8b499b67");
    }

    public static void main(String[] args) {
        try {
            // 设置代理服务器地址和端口号
            Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 33210));

//            getAllFc2(proxy);
//            getAll(proxy);
//            getItem(proxy);
            getMagic(proxy);
//            getMagicUrl(proxy);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    private static void getItem(Proxy proxy) throws IOException {
        String url = "https://www.sehuatang.net/thread-2066469-1-1.html";
        Document doc = Jsoup.connect(url)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0")
                .proxy(proxy)
                .header("Referer", "https://www.sehuatang.net/").cookie("Cookie", "cPNj_2132_saltkey=ujN4JdmS; _safe=vqd37pjm4p5uodq339yzk6b7jdt6oich; cPNj_2132_lastvisit=1715840410; cPNj_2132_lastfp=27cf7dc10da7360a67316c1c8b499b67; cPNj_2132_st_t=0%7C1715844028%7Cb95bab315ce56a11a6bafb861ce0adfa; cPNj_2132_atarget=1; cPNj_2132_forum_lastvisit=D_43_1715844028; cPNj_2132_visitedfid=43; cPNj_2132_lastact=1715845692%09forum.php%09viewthread; cPNj_2132_st_p=0%7C1715845692%7Cd9ee35e847a7298e358af147ed2e6c87; cPNj_2132_viewid=tid_2066469")
                .get();

        // 打印页面的 HTML 内容
        System.out.println(doc.outerHtml());
    }

    private static List<Map<String, Object>> getAll(Proxy proxy) throws IOException {
        // 目标页面的 URL
        String url = "https://www.sehuatang.net/forum-42-1.html";

        // 发送 GET 请求并获取页面内容
        Document doc = Jsoup.connect(url)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0")
                .proxy(proxy)
                .header("Referer", "https://www.sehuatang.net/").cookie("Cookie", "cPNj_2132_saltkey=ujN4JdmS; _safe=vqd37pjm4p5uodq339yzk6b7jdt6oich; cPNj_2132_lastvisit=1715840410; cPNj_2132_lastact=1715844010%09index.php%09; cPNj_2132_lastfp=27cf7dc10da7360a67316c1c8b499b67")
                .get();

        // 打印页面的 HTML 内容
//        System.out.println(doc.outerHtml());

        // 查找所有的 li 元素
        Elements liElements = doc.select("ul#waterfall li");

        // 创建一个集合来存储结果
        List<Map<String, Object>> links = new ArrayList<>();

        // 遍历 li 元素，提取其中的 a 标签的 href 和 title 属性
        for (Element li : liElements) {
            Element aTag = li.selectFirst("a"); // 获取 li 元素下的第一个 a 标签

            // 提取 href 和 title 属性
            String href = aTag.attr("href");
            String title = aTag.attr("title");

            Element aImg = li.selectFirst("img"); // 获取 li 元素下的第一个 img 标签

            // 提取 src 和 alt 属性
            String src = aImg.attr("src");
            String alt = aImg.attr("alt");

            HashMap<String, Object> map = new HashMap<>();
            map.put("href", href);
            map.put("title", title);
            map.put("src", src);
            map.put("alt", alt);

            // 创建 LinkInfo 对象并添加到集合中
            links.add(map);
        }

        for (Map<String, Object> link : links) {
            System.out.println(link.get("title") + "----" + link.get("href") + "----" + link.get("alt") + "----"  + link.get("src"));
        }

        return links;
    }

    private static List<Map<String, Object>> getAllFc2(Proxy proxy) throws IOException {
        // 创建一个集合来存储结果
        List<Map<String, Object>> links = new ArrayList<>();

        // 目标页面的 URL
        String url = "https://www.sehuatang.net/forum.php?mod=forumdisplay&fid=36&typeid=368&typeid=368&filter=typeid&page=5";

        // 发送 GET 请求并获取页面内容
        Document doc = Jsoup.connect(url)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36")
                .proxy(proxy).header("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7")
                .header("Referer", "https://www.sehuatang.net/forum.php?").cookie("Cookie", "cPNj_2132_saltkey=ObhNB7JN; _safe=vqd37pjm4p5uodq339yzk6b7jdt6oich; cPNj_2132_lastvisit=1715851718; cPNj_2132_lastfp=b62bc8a527bc0376521d22ba71e7233d; cPNj_2132_atarget=1; cPNj_2132_visitedfid=36; cf_clearance=5WNz7uWUFVF2gkja1DP0IOBgwTsG64TsEvJ46JYC71Y-1715855738-1.0.1.1-BzB8By1Ul_04XMd6LTKqGeC5E9c_v3KqCrb_03iPfSWS3kcfdNuUmPkwhwDOvtql7kTWbc_GwvLRA3dc3Ij4kQ; cPNj_2132_st_t=0%7C1715855834%7C89f983592cd8374d084f49da2b579df2; cPNj_2132_forum_lastvisit=D_36_1715855834; cPNj_2132_lastact=1715855981%09forum.php%09ajax")
                .get();
        // 打印页面的 HTML 内容
        System.out.println(doc.outerHtml());

        // 查找所有的 a 标签
        Elements aTags = doc.select("a.s.xst");

        // 遍历 a 标签，获取其中的文字和 href 属性
        for (Element aTag : aTags) {
            String text = aTag.text(); // 获取标签内的文字
            String href = aTag.attr("href"); // 获取 href 属性的值

            // 打印结果
            System.out.println("Text: " + text);
            System.out.println("Href: " + href);
            System.out.println();

            HashMap<String, Object> map = new HashMap<>();
            map.put("text", text);
            map.put("href", href);

            // 创建 LinkInfo 对象并添加到集合中
            links.add(map);
        }

        for (Map<String, Object> link : links) {
            System.out.println(link.get("text") + "----" + link.get("href"));
        }

        return links;
    }

    private static void getMagic(Proxy proxy) throws IOException {
        String url = "https://cdn.btlm.top:65533/s?word=FC2";
        Document doc = Jsoup.connect(url)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0")
                .proxy(proxy)
                .header("Referer", "https://cdn.btlm.top:65533/s?word=985469")
                .get();

        // 打印页面的 HTML 内容
        System.out.println(doc.outerHtml());
    }

    private static void getMagicUrl(Proxy proxy) throws IOException {
        String url = "https://sn.btlm.one/hash/b822faf04542ab7f3b39db007f9818481760c7a1";
        Document doc = Jsoup.connect(url)
                .userAgent("Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/124.0.0.0 Safari/537.36 Edg/124.0.0.0")
                .proxy(proxy)
                .header("Referer", "https://sn.btlm.one/")
                .get();
//        magnet:?xt=urn:btih:b822faf04542ab7f3b39db007f9818481760c7a1
        // 打印页面的 HTML 内容
        System.out.println(doc.outerHtml());
    }


}
